package com.thzy.bbs.setting;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.appcompat.BuildConfig;

import com.thzy.bbs.activity.MainActivity;
import com.thzy.bbs.callBacks.PhotoCallBack;

import java.io.File;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ImageLoader;
import cn.finalteam.galleryfinal.ThemeConfig;

/**
 * WbeView功能（主要跳转各种应用）
 *
 * @author Administrator
 */
public class SetMainSkip {
    private MainActivity mainActivity;

    public SetMainSkip(MainActivity mainActivity) {
        super();
        this.mainActivity = mainActivity;
    }

    /**
     * 自定义多选照片（多选）
     *
     * @author Lirui
     */
    public void openPictures(int nu, int type, String userId) {
        //配置主题
        //ThemeConfig.CYAN
        ThemeConfig theme = new ThemeConfig.Builder()
                .build();
        //配置功能
        FunctionConfig functionConfig = new FunctionConfig.Builder()
                .setMutiSelectMaxSize(nu)//设置选择图片数
                .setEnableCamera(true)//开启相机
                .setEnableRotate(true)//开启选择功能
                .build();
        //配置imageloader
        ImageLoader imageloader = new PhotoSetting();
        //设置核心配置信息
        CoreConfig coreConfig = new CoreConfig.Builder(mainActivity, imageloader, theme)
                .setDebug(BuildConfig.DEBUG)
                .setFunctionConfig(functionConfig)
                .build();
        GalleryFinal.init(coreConfig);
        GalleryFinal.openGalleryMuti(type, functionConfig, new PhotoCallBack(userId));
    }

    /**
     * 清除缓存
     *
     * @return
     */
    public Boolean clearWebViewCache() {
        Boolean isOk = true;
        // 清理Webview缓存数据库,缓存数据信息会存入数据库中
        try {
            mainActivity.deleteDatabase("webview.db");
            mainActivity.deleteDatabase("webviewCache.db");
        } catch (Exception e) {
            e.printStackTrace();
            isOk = false;
        }
        // WebView 缓存文件
        File appCacheDir = new File(mainActivity.getFilesDir()
                .getAbsolutePath());
        File webviewCacheDir = new File(mainActivity.getCacheDir()
                .getAbsolutePath());
        // 删除webview 缓存目录
        if (webviewCacheDir.exists()) {
            deleteFile(webviewCacheDir);
        }
        // 删除webview 缓存 缓存目录
        if (appCacheDir.exists()) {
            deleteFile(appCacheDir);
        }
        return isOk;
    }

    /**
     * 递归删除 文件/文件夹
     *
     * @param file
     */
    private Boolean deleteFile(File file) {
        Boolean isOk = true;
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                File files[] = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    deleteFile(files[i]);
                }
            }
            file.delete();
        } else {
            isOk = false;
        }
        return isOk;
    }

    /**
     * 跳转googlePlay
     */
    public void goToGooglePlay() {
        final String appPackageName = mainActivity.getPackageName(); // getPackageName()
        // from
        // Context or Activity
        // object
        try {
            mainActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                    .parse("market://details?id=" + appPackageName)));
        } catch (android.content.ActivityNotFoundException anfe) {
            mainActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                    .parse("https://play.google.com/store/apps/details?id="
                            + appPackageName)));
        }
    }

}
